package orion.cricScoreCard.Activity;

import orion.cricScoreCard.Database.DBAdapter;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class CricScoreCardActivity extends Activity implements OnClickListener {

	private EditText editTeam1;
	private EditText editTeam2;
	private EditText editOver;
	private Button btnToss;

	private DBAdapter dbAdapter;
	private String toss;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		showOptions();
		init();
		btnToss.setOnClickListener(this);
	}

	private void init() {
		editTeam1 = (EditText) findViewById(R.id.edittxtTeam1);
		editTeam2 = (EditText) findViewById(R.id.edittxtTeam2);
		editOver = (EditText) findViewById(R.id.edittxtOvers);
		btnToss = (Button) findViewById(R.id.btnToss);
		dbAdapter = new DBAdapter(getApplicationContext());
	}

	private void showOptions() {
		new AlertDialog.Builder(this)
				.setIcon(R.drawable.ic_launcher)
				.setTitle("CricScore")
				.setMessage("Select One")
				.setPositiveButton("History",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {

							}
						})
				.setNegativeButton("New Game",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {

							}
						}).show();

	}

	private void showTossOptions(final String opt1, final String opt2,
			final String overs) {
		new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher)
				.setTitle("CricScore").setMessage("Toss Result")
				.setPositiveButton(opt1, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						toss = opt1;
						showBattingOption(toss, opt2, overs);
					}
				})
				.setNegativeButton(opt2, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						toss = opt2;
						showBattingOption(toss, opt1, overs);
					}
				}).show();

	}

	private void showBattingOption(final String toss, final String opt1,
			final String overs) {
		new AlertDialog.Builder(this)
				.setIcon(R.drawable.ic_launcher)
				.setTitle("CricScore")
				.setMessage("Team " + toss + "'s Option:")
				.setPositiveButton("Batting",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dbAdapter.insert_match(toss, opt1, overs, toss,
										"");
								dbAdapter.closeDB();
								Intent playerInt = new Intent(
										getApplicationContext(),
										TeamDetails.class);
								Bundle data = new Bundle();
								data.putString("Batting", toss);
								data.putString("Bowling", opt1);
								data.putString("Overs", overs);
								playerInt.putExtras(data);
								startActivity(playerInt);
							}
						})
				.setNegativeButton("Bowling",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dbAdapter.insert_match(opt1, toss, overs, toss,
										"");
								dbAdapter.closeDB();
								Intent playerInt = new Intent(
										getApplicationContext(),
										TeamDetails.class);
								Bundle data = new Bundle();
								data.putString("Batting", opt1);
								data.putString("Bowling", toss);
								data.putString("Overs", overs);
								playerInt.putExtras(data);
								startActivity(playerInt);

							}
						}).show();
	}

	@Override
	public void onClick(View v) {
		String overs = editOver.getText().toString();
		String team1 = editTeam1.getText().toString();
		String team2 = editTeam2.getText().toString();
		switch (v.getId()) {
		case R.id.btnToss:

			showTossOptions(team1, team2, overs);

			break;
		}

	}
}